import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class labor1 {
	private static Connection con;
	private static Statement stmt;
	public static void main(String[] args) {
		connect();
		//getTable();
		userAction();
	}
	private static void userAction(){
		BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Geben Sie SQL ein: ");
		String zeile = null;
		try {
			zeile = console.readLine();
		} catch (IOException e) {
			// Sollte eigentlich nie passieren
			e.printStackTrace();
		}
		//System.out.println("Ihre Eingabe war: " + zeile);
		try {
			boolean b = stmt.execute(zeile);
			if(b){
				try {	
					ResultSet res = stmt.executeQuery(zeile);
					while(res.next()){
						System.out.println(res.getString("NAME") + " - " + res.getString("EMAIL"));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e){
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private static void connect(){
		try {
			con = DriverManager.getConnection("jdbc:odbc:tokuit01");
			stmt = con.createStatement();
		} catch (SQLException e){
			e.printStackTrace();
		}
	}
	
	private void createTable(){
		try {
			stmt.executeUpdate("CREATE TABLE USER (NAME VARCHAR(200), EMAIL VARCHAR(200))");
			stmt.executeUpdate("INSERT INTO USER (NAME, EMAIL) VALUES('Tobias', 'tobias@mail.com')");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private static void getTable(){
		try {	
			ResultSet res = stmt.executeQuery("SELECT * FROM USER");
			while(res.next()){
				System.out.println(res.getString("NAME") + " - " + res.getString("EMAIL"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
	}
}